package com.bcdat.bcdatbrace.bcdatEsSearch.service.impl;

import com.bcdat.bcdatbrace.bcdatEsSearch.document.ProductDocument;
import com.bcdat.bcdatbrace.bcdatEsSearch.repository.ProductDocumentRepository;
import com.bcdat.bcdatbrace.bcdatEsSearch.service.EsSearchService;
import com.bcdat.bcdatbrace.utils.ResultVOUtil;
import com.bcdat.bcdatbrace.utils.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * elasticsearch 搜索引擎 service实现
 * @author lsy
 * @version 0.1
 * @date 2019/09/30 15:33
 */
@Service
public class EsSearchServiceImpl extends BaseSearchServiceImpl<ProductDocument> implements EsSearchService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;
    @Resource
    private ProductDocumentRepository productDocumentRepository;

    @Override
    public void save(ProductDocument ... productDocuments) {
        elasticsearchTemplate.putMapping(ProductDocument.class);
        if(productDocuments.length > 0){
            /*Arrays.asList(productDocuments).parallelStream()
                    .map(productDocumentRepository::save)
                    .forEach(productDocument -> log.info("【保存数据】：{}", JSON.toJSONString(productDocument)));*/
//            log.info("【保存索引】：{}",JSON.toJSONString(productDocumentRepository.saveAll(Arrays.asList(productDocuments))));
        }
    }

    @Override
    public ResultVO delete(Map map) {
        try{
            String id = map.get("id").toString();
            productDocumentRepository.deleteById(id);
            return ResultVOUtil.success();
        }catch (Exception e){
            log.info("=================异常信息=================:"+e);
            return ResultVOUtil.error();
        }
    }

    @Override
    public ResultVO deleteAll() {
        try{
            productDocumentRepository.deleteAll();
            return ResultVOUtil.success();
        }catch (Exception e){
            log.info("=================异常信息=================:"+e);
            return ResultVOUtil.error();
        }
    }

    @Override
    public ResultVO getById(Map map) {
        try{
            String id = map.get("id").toString();
            return ResultVOUtil.success(productDocumentRepository.findById(id).get());
        }catch (Exception e){
            log.info("=================异常信息=================:"+e);
            return ResultVOUtil.error();
        }
    }

    @Override
    public ResultVO getAll() {
        try{
            List<ProductDocument> list = new ArrayList<>();
            productDocumentRepository.findAll().forEach(list::add);
            return ResultVOUtil.success(list);
        }catch (Exception e){
            log.info("=================异常信息=================:"+e);
            return ResultVOUtil.error();
        }

    }


    @Override
    public ResultVO addProductDocument(ProductDocument productDocument) {
        try{
            productDocumentRepository.save(productDocument).toString();
            return ResultVOUtil.success();
        }catch (Exception e){
            log.info("=================异常信息=================:"+e);
            return ResultVOUtil.error();
        }

    }


}
